package com.solace.thumbnailator.example;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Thumbnailator {
    public static void main(String[] args) throws IOException {
        // 需要压缩的图片
        File inFile = new File("C:/images/test.jpg");
        // 压缩完的存放地址
        String outFile = "C:/images/test.jpg";
        // 图片裁剪
        Thumbnails.of(inFile).sourceRegion(0,0,400,400).size(200,200).toFile(outFile);
        // 修改像素
        Thumbnails.of(inFile).size(200,200).toFile(outFile);
        // 缩小
        Thumbnails.of(inFile).scale(0.5f).toFile(outFile);
        // 扩大
        Thumbnails.of(inFile).scale(2f).toFile(outFile);
        // 压缩质量
        Thumbnails.of(inFile).outputQuality(0.5f).toFile(outFile);
        // 旋转
        Thumbnails.of(inFile).rotate(90).toFile(outFile);
        // 图片水印
        File waterFile = new File("C:/images/water.jpg");
        Thumbnails.of(inFile).watermark(Positions.BOTTOM_CENTER, ImageIO.read(waterFile),0.5f).toFile(outFile);
        // 文字水印
        BufferedImage bi = new BufferedImage(80,30,BufferedImage.TYPE_INT_BGR);
        Graphics2D g = bi.createGraphics();
        g.setColor(Color.lightGray);
        g.drawRect(0,0,50,10);
        g.drawString("文字水印", 20,20);
        Thumbnails.of(inFile).watermark(Positions.BOTTOM_LEFT, bi, 0.5f).toFile(outFile);
        // 转化图片格式
        Thumbnails.of(inFile).outputFormatType("png").toFile(outFile);
    }

}